home *** CD-ROM | disk | FTP | other *** search
- /* Functions for maintaining a menu containing a list of windows. Selecting
- an item from the menu brings the window with that title to the front.
-
- 94/01/06 aih
- - an array is used to associate windows with menu items, instead of
- relying only on the menu item's title, which used to cause conflicts
- if more than one window used the same title
-
- 93/03/06 AIH
- - The current window has a check-mark placed next to its name
-
- 92/02/22 AIH
- - Improved event handling
-
- 91/06/13 AIH
- - Inserting a menu item with an empty title is ignored, so InsMenuItem
- is called with the title string just before calling SetItem
-
- 91/05/31 AIH
- - SetItem is used to avoid interpreting characters in a window's title
- as menu metacharacters
-
- 91/05/20 AIH
- - Trying to get the window insertion to work correctly
-
- 91/05/14 Ari Halberstadt (AIH)
- - Created this library. */
-
- #include <string.h>
- #include <Packages.h>
- #include "ArrayListLib.h"
- #include "EventLib.h"
- #include "MenuLib.h"
- #include "ResourceConstantsLib.h"
- #include "WindowLib.h"
- #include "WindowMenuLib.h"
-
- /* The first item in the windows menu containing a window title. You
- could change this so that the first few items would do various
- window actions, such as Tile, Clean Up, Close All, etc. */
- #define FIRST_WINDOW_ITEM (1)
-
- static ArrayListHandle gWindowMenuArray;
-
- /* return menu item containing window, or zero if not found */
- static short WinMenuFind(WindowPtr find)
- {
- WindowPtr window = NULL;
- short i = 0, n;
-
- if (find && gWindowMenuArray) {
- n = ArrayListCount(gWindowMenuArray);
- for (i = 0; i < n; i++) {
- ArrayListGet(gWindowMenuArray, i, &window);
- if (window == find) {
- i += FIRST_WINDOW_ITEM;
- break;
- }
- }
- }
- return(window == find ? i : 0);
- }
-
- /* add the title of the window to the windows menu */
- void WinMenuAdd(WindowPtr window)
- {
- Str255 title; /* title of window */
- Str255 item; /* text of current menu item */
- short nitems; /* number of items in menu */
- short i; /* index to current menu item */
- MenuHandle menu; /* the windows menu */
-
- require(WinValid(window));
- require(WinLayer(window) == WIN_LAYER_DOCUMENT);
- if (! WinMenuFind(window)) {
- /* insert window's title into menu (in sorted order) */
- GetWTitle(window, title);
- menu = MenuCmdHandle(CMD_WINDOW);
- nitems = CountMItems(menu);
- for (i = 1; i <= nitems; i++) {
- GetItem(menu, i, item);
- if (IUCompString(title, item) <= 0)
- break;
- }
- InsMenuItem(menu, (StringPtr) "\pany non-empty title", i - 1);
- SetItem(menu, i, title);
- /* insert window into array */
- if (! gWindowMenuArray)
- gWindowMenuArray = ArrayListBegin(sizeof(WindowPtr));
- ArrayListInsert(gWindowMenuArray, i - FIRST_WINDOW_ITEM);
- ArrayListSet(gWindowMenuArray, i - FIRST_WINDOW_ITEM, &window);
- }
- ensure(WinMenuFind(window) > 0);
- }
-
- /* remove the window from the windows menu */
- void WinMenuRemove(WindowPtr window)
- {
- short item;
-
- require(WinValid(window));
- require(WinLayer(window) == WIN_LAYER_DOCUMENT);
- item = WinMenuFind(window);
- if (item) {
- DelMenuItem(MenuCmdHandle(CMD_WINDOW), item);
- ArrayListDelete(gWindowMenuArray, item - 1);
- }
- }
-
- /* adjust the windows menu */
- void WinMenuAdjustMenu(void)
- {
- MenuHandle menu;
-
- menu = MenuCmdHandle(CMD_WINDOW);
- if (! WinModalHasFocus() && CountMItems(menu) > 0) {
- MenuCmdEnable(CMD_WINDOW);
- CheckItem(menu, WinMenuFind(WinFirstVisible(WIN_LAYER_DOCUMENT)), true);
- }
- }
-
- /* handle a selection from the windows menu by selecting the
- corresponding window */
- Boolean WinMenuDoMenu(const MenuPickType *pick)
- {
- Boolean handled = false;
- WindowPtr window;
-
- if (pick->cmd == CMD_WINDOW) {
- ArrayListGet(gWindowMenuArray, pick->item - FIRST_WINDOW_ITEM, &window);
- WinSelect(window);
- handled = true;
- }
- return(handled);
- }
-